package com.share.example.stock.web;

import com.share.common.common.Page;
import com.share.common.common.Result;
import com.share.common.utils.DateUtil;
import com.share.example.stock.model.rsp.StockBaseInfoPageReq;
import com.share.example.stock.service.IStockBaseInfoService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2023-06-10
 */
@RestController
@RequestMapping("/stockbaseinfo")
public class StockBaseInfoController {
    @Autowired
    private IStockBaseInfoService iStockBaseInfoService;

    //分页
    @RequestMapping("/pageList")
    public Result pageList(StockBaseInfoPageReq req) {
        Page page = iStockBaseInfoService.pageList(req);
        return Result.success(page);
    }

    /**
     * 同步下载指定的code
     *
     * @param codes
     * @return
     */
    @RequestMapping("/down")
    public Result downCodes(String codes) {
        List list = new LinkedList();
        if (StringUtils.isEmpty(codes)) {
            list = iStockBaseInfoService.listCodeByBasePullCount();
        } else {
            String[] split = codes.split(",");
            for (String str : split) {
                if (StringUtils.isNoneBlank(str)) {
                    list.add(str);
                }
            }
        }
        iStockBaseInfoService.resetErrorCount(0);
        Object obj = iStockBaseInfoService.downAndUpdateStockInfo(list);
        return Result.success(obj);
    }

    @ApiOperation("首次初始化必须调用")
    @RequestMapping("/init")
    public Result init() {
        Object obj = iStockBaseInfoService.downAllCode();
        return Result.success(obj);
    }

    @ApiOperation("更新dn的数据")
    @RequestMapping("/updateDn")
    public Result updateDn(String date) {
        if (StringUtils.isBlank(date)) {
            date = DateUtil.getDate();
        }
        Object obj = iStockBaseInfoService.updateDn(date);
        return Result.success(obj);
    }
}
